from mxupy import DatabaseHelper
import bigOAINet as bigo
import OBEvolutionAI as obe
from peewee import *

def install():
    """初始化数据库，创建所有表并填充示例数据"""
    
    # 获取所有控制器实例
    school_control = obe.SchoolControl.inst()
    college_control = obe.CollegeControl.inst()
    dept_control = obe.DeptControl.inst()
    major_control = obe.MajorControl.inst()
    student_control = obe.StudentControl.inst()
    major_teacher_control = obe.MajorTeacherControl.inst()
    course_control = obe.CourseControl.inst()
    course_teacher_control = obe.CourseTeacherControl.inst()
    course_elective_control = obe.CourseElectiveControl.inst()
    schedule_control = obe.ScheduleControl.inst()
    schedule_user_control = obe.ScheduleUserControl.inst()
    discuss_control = obe.DiscussControl.inst()
    discuss_user_control = obe.DiscussUserControl.inst()
    assignment_control = obe.AssignmentControl.inst()
    assignment_user_control = obe.AssignmentUserControl.inst()
    test_item_control = obe.TestItemControl.inst()
    test_item_user_control = obe.TestItemUserControl.inst()
    check_in_control = obe.CheckInControl.inst()
    check_in_user_control = obe.CheckInUserControl.inst()
    
    # 创建所有表
    print("开始创建数据库表...")
    
    school_control.create_table()
    college_control.create_table()
    dept_control.create_table()
    major_control.create_table()
    student_control.create_table()
    major_teacher_control.create_table()
    course_control.create_table()
    course_teacher_control.create_table()
    course_elective_control.create_table()
    schedule_control.create_table()
    schedule_user_control.create_table()
    discuss_control.create_table()
    discuss_user_control.create_table()
    assignment_control.create_table()
    assignment_user_control.create_table()
    test_item_control.create_table()
    test_item_user_control.create_table()
    check_in_control.create_table()
    check_in_user_control.create_table()
    
    print("数据库表创建完成")
    
    # 填充示例数据
    print("开始填充示例数据...")
    
    # 创建一个学校
    school_result = school_control.add({
        'name': '示例大学'
    })
    
    if school_result.success:
        school_id = school_result.data.schoolId
        print(f"创建学校，ID: {school_id}")
        
        # 创建学院
        college_result = college_control.add({
            'name': '计算机学院',
            'schoolId': school_id
        })
        
        if college_result.success:
            college_id = college_result.data.collegeId
            print(f"创建学院，ID: {college_id}")
            
            # 创建系
            dept_result = dept_control.add({
                'name': '计算机科学与技术系',
                'collegeId': college_id
            })
            
            if dept_result.success:
                dept_id = dept_result.data.deptId
                print(f"创建系，ID: {dept_id}")
                
                # 创建专业
                major_result = major_control.add({
                    'name': '计算机科学与技术',
                    'deptId': dept_id
                })
                
                if major_result.success:
                    major_id = major_result.data.majorId
                    print(f"创建专业，ID: {major_id}")
    
    # 创建课程
    course_result = course_control.add({
        'name': '人工智能导论',
        'desc': '介绍人工智能的基本概念和应用',
        'logo': '/images/ai_intro.jpg',
        'isOpen': 1,
        'robotUserId': 1
    })
    
    if course_result.success:
        course_id = course_result.data.courseId
        print(f"创建课程，ID: {course_id}")
        
        # 创建课堂
        schedule_result = schedule_control.add({
            'name': '人工智能导论-001',
            'invitationCode': 'AI2025',
            'courseId': course_id,
            'robotUserId': 1
        })
        
        if schedule_result.success:
            schedule_id = schedule_result.data.scheduleId
            print(f"创建课堂，ID: {schedule_id}")
            
            # 创建讨论
            discuss_result = discuss_control.add({
                'title': '什么是人工智能？',
                'answer': '人工智能是计算机科学的一个分支，旨在创建能够执行通常需要人类智能的任务的系统。',
                'criteria': '定义准确、表述清晰',
                'createrId': 1,
                'courseId': course_id
            })
            
            if discuss_result.success:
                discuss_id = discuss_result.data.discussId
                print(f"创建讨论，ID: {discuss_id}")
                
                # 创建作业
                assignment_result = assignment_control.add({
                    'name': '第一章作业',
                    'answer': '作业参考答案',
                    'criteria': '完成度100%',
                    'createrId': 1,
                    'courseId': course_id
                })
                
                if assignment_result.success:
                    assignment_id = assignment_result.data.assignmentId
                    print(f"创建作业，ID: {assignment_id}")
                    
                    # 创建试题
                    test_item_result = test_item_control.add({
                        'content': '人工智能的英文缩写是什么？',
                        'answer': 'AI',
                        'type': 'blank',
                        'createrId': 1,
                        'courseId': course_id
                    })
                    
                    if test_item_result.success:
                        test_item_id = test_item_result.data.testItemId
                        print(f"创建试题，ID: {test_item_id}")
    
    print("示例数据填充完成")

if __name__ == "__main__":
    install()